In [1]:
%reload_ext autotime
import pandas as pd
import requests
from pprint import pprint
import json
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
from tqdm.auto import tqdm
pd.options.plotting.backend = "plotly"
pd.set_option("display.max_columns", None)
pd.set_option("display.max_colwidth", 100)
⌛ 1.44 µs (2024-12-11T12:18:00)
2024-12-11 12:18:03.892596: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2024-12-11 12:18:03.911171: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-12-11 12:18:03.931713: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-12-11 12:18:03.938723: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered 2024-12-11 12:18:03.959172: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2024-12-11 12:18:04.791471: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
In [2]:
df = pd.read_csv("results.csv").drop_duplicates(subset="panoid")
df
✔️ 20.5 ms (2024-12-11T12:18:05/2024-12-11T12:18:05)
Out[2]:
| Index | pid | n | time | anxiousness | latitude | longitude | geometry | panoid | panolat | panolon | panoyear | panomonth | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | P20001 | 1 | 2023-04-25T02:51:42Z | 0 | -36.924795 | 174.738044 | POINT (174.7380435 -36.92479483) | QEpZV7bnO2mBfp0weMUKEg | -36.924730 | 174.737826 | 2012 | 4 |
| 10 | 10 | P20001 | 11 | 2023-04-24T00:42:25Z | 0 | -36.924837 | 174.737948 | POINT (174.7379477 -36.92483659) | pUw8PmVPYZBTW26mq2DAfw | -36.924785 | 174.737728 | 2012 | 4 |
| 13 | 13 | P20006 | 1 | 2023-06-03T02:45:55Z | 3 | -36.892203 | 174.740125 | POINT (174.7401253 -36.89220256) | omb98QNjTPWi0uUfMsmYeg | -36.892621 | 174.739961 | 2024 | 5 |
| 14 | 15 | P20009 | 2 | 2023-05-17T04:54:48Z | 3 | -36.923191 | 174.748620 | POINT (174.7486203 -36.92319093) | E7B5AV3DQ1rYWDClVRo8Zg | -36.923194 | 174.748831 | 2024 | 5 |
| 17 | 19 | P20009 | 6 | 2023-05-19T22:28:51Z | 1 | -36.923260 | 174.748655 | POINT (174.748655 -36.92325959) | KCTcsxYCIm41XdzkYEYUQw | -36.923286 | 174.748840 | 2024 | 5 |
| 19 | 21 | P20015 | 1 | 2023-05-17T07:34:00Z | 5 | -36.921603 | 174.747739 | POINT (174.747739 -36.92160252) | ESE0Slg2IO7Vf3QdBhETkg | -36.921626 | 174.747253 | 2024 | 5 |
| 22 | 25 | P20021 | 2 | 2023-06-04T02:33:49Z | 6 | -37.675727 | 175.209414 | POINT (175.2094142 -37.67572725) | NQi8eh4MBHprJGCpl9t1EQ | -37.675820 | 175.209455 | 2023 | 11 |
| 23 | 26 | P20021 | 3 | 2023-06-05T21:49:46Z | 3 | -36.894889 | 174.742775 | POINT (174.7427751 -36.89488899) | qgtMQGHZWUUIBCa8JgbBhA | -36.895076 | 174.742734 | 2024 | 5 |
| 24 | 27 | P20021 | 4 | 2023-06-06T02:29:11Z | 5 | -36.894854 | 174.742929 | POINT (174.7429285 -36.89485419) | T4yBf38jq472FmvtzEtI_w | -36.895101 | 174.742848 | 2024 | 5 |
| 25 | 30 | P20022 | 3 | 2023-04-25T06:42:09Z | 1 | -36.913380 | 174.731288 | POINT (174.7312875 -36.91337995) | do2cpZfBTwfxHkWnQkyL3A | -36.913440 | 174.731310 | 2024 | 7 |
| 26 | 31 | P20022 | 4 | 2023-04-25T22:31:15Z | 6 | -36.880662 | 174.707832 | POINT (174.7078325 -36.88066162) | vgFJoPSu-4SSfUKqz4BGgA | -36.880785 | 174.707410 | 2024 | 6 |
| 27 | 33 | P20022 | 6 | 2023-04-24T03:16:17Z | 4 | -36.852978 | 174.767267 | POINT (174.7672665 -36.85297814) | 0dU2CZ_GXhuhodJJbXVHSA | -36.852731 | 174.767062 | 2017 | 2 |
| 28 | 34 | P20027 | 1 | 2023-05-27T21:50:10Z | 6 | -36.892136 | 174.736943 | POINT (174.7369429 -36.89213617) | ody-NBwD6S0562GUtROqtg | -36.891996 | 174.737012 | 2024 | 6 |
| 35 | 41 | P20027 | 8 | 2023-05-30T21:17:36Z | 2 | -36.887537 | 174.736875 | POINT (174.7368754 -36.88753691) | Vy5UxGKwH8RxSoG2tFB94Q | -36.887653 | 174.737623 | 2024 | 6 |
| 38 | 44 | P20027 | 11 | 2023-06-01T05:24:54Z | 3 | -36.888974 | 174.735651 | POINT (174.7356508 -36.88897381) | IUzaXGEkzyOV-oEgTQaHhA | -36.888938 | 174.735757 | 2023 | 1 |
| 39 | 45 | P20027 | 12 | 2023-06-02T03:42:14Z | 3 | -36.887732 | 174.735789 | POINT (174.7357892 -36.88773177) | uPRCtL-OvwI3f_lXNBZtdw | -36.888043 | 174.735442 | 2023 | 1 |
| 44 | 50 | P20033 | 1 | 2023-05-03T08:19:03Z | 0 | -36.978477 | 174.830027 | POINT (174.8300269 -36.9784771) | BoCOn1VpFGrbXlyX3EKZ6g | -36.978467 | 174.830275 | 2022 | 8 |
| 46 | 52 | P20033 | 3 | 2023-05-04T02:17:26Z | 0 | -36.978365 | 174.830125 | POINT (174.8301251 -36.97836488) | tbfXbYFHITDw8p7vCFU3KA | -36.978564 | 174.830225 | 2022 | 8 |
In [4]:
# Loading this model needs about 22.69GB of GPU memory
model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_id)
✔️ 16.7 s (2024-12-11T12:18:36/2024-12-11T12:18:53)
Downloading shards: 0%| | 0/5 [00:00<?, ?it/s]
The model weights are not tied. Please use the `tie_weights` method before using the `infer_auto_device` function.
Loading checkpoint shards: 0%| | 0/5 [00:00<?, ?it/s]
tokenizer_config.json: 0%| | 0.00/55.8k [00:00<?, ?B/s]
chat_template.json: 0%| | 0.00/5.09k [00:00<?, ?B/s]
In [7]:
for row in tqdm(df.head(10).itertuples(index=False)):
panoid = row.panoid
image = Image.open(f"panoramas/{panoid}.png")
display(image)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": """
This image is a panorama from Google Street View.
From the image, extract the following information, in JSON format:
green: Percentage of the image that is green space (e.g. parks, gardens, trees, grass etc.). A number from 0-100.
environment: Classify the nature of the environment in this image. Built up/green/residential/shops/cafes?. A string.
water: If you see any streams/ponds/rivers/ocean in the image, estimate the distance to the water in meters. A number. If there is no water, return 0.
obscured: Proportion of view obscured by buildings (how much of total line of sight is blocked by buildings in close proximity). A number from 0-100.
people: the number of people you see in the image
cars: the number of cars you see in the image
bikes: the number of bikes you see in the image
Do not include comments in your JSON response. Only respond with the JSON object. Make sure the JSON is valid.
"""},
{"type": "image"},
]
}
]
input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(
image,
input_text,
add_special_tokens=False,
return_tensors="pt"
).to(model.device)
for retry in range(3):
output = model.generate(**inputs, max_new_tokens=5000)
result = processor.decode(output[0])
result = result[result.rindex("<|end_header_id|>") + len("<|end_header_id|>"):].strip().replace("<|eot_id|>", "")
print("Output:")
try:
result = json.loads(result)
pprint(result)
print("\n")
break
except json.JSONDecodeError:
print(f"Unable to parse: {result}")
✔️ 26.2 s (2024-12-11T12:38:51/2024-12-11T12:39:18)
0it [00:00, ?it/s]
Output:
{'bikes': 0,
'cars': 2,
'environment': 'residential',
'green': 40,
'obscured': 60,
'people': 1,
'water': 0}
Output:
{'bikes': 0,
'cars': 3,
'environment': 'residential',
'green': 30,
'obscured': 0,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 1,
'environment': 'residential',
'green': 90,
'obscured': 10,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 3,
'environment': 'residential',
'green': 55,
'obscured': 45,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 4,
'environment': 'residential',
'green': 50,
'obscured': 50,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 4,
'environment': 'residential',
'green': 70,
'obscured': 10,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 1,
'environment': 'residential',
'green': 70,
'obscured': 0,
'people': 0,
'water': 0}
Output:
{'bikes': 0,
'cars': 8,
'environment': 'residential',
'green': 75,
'obscured': 20,
'people': 0,
'water': 0}